package model;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import utils.DateUtils;
import form.RegistrationForm;

public class DBHelper {

	private static Logger logger = Logger.getLogger(DBHelper.class);

	private static List<User> userList = new ArrayList<User>();

	static {
		// Adding some users to the "database".
		addUser(userList, "aaa@gmail.com", "11111111", 1, 30,
				"01/08/2013 12:05:23.123");

		//
		addUser(userList, "bbb@gmail.com", "22222222", 0, 50,
				"29/02/2012 09:59:59.999");

		//
		addUser(userList, "ccc@yahoo.com", "33333333", 1, 18,
				"29/03/2013 22:15:33.999");
	}

	public static List<User> getUserList() {

		return userList;
	}

	private static void addUser(List<User> userList, String email, String pwd,
			int tipoUsuario, int age, String signUpDate) {
		User user = new User();

		user.setId(userList.size());
		user.setEmail(email);
		user.setPassword(pwd);
		user.setTipoUsuario(tipoUsuario);
		user.setAge(age);
		try {
			user.setSignUpDate(DateUtils.toDate(signUpDate));
		} catch (ParseException e) {
			logger.error("addUser received a wrong signUpDate=" + signUpDate, e);
			user.setSignUpDate(null);
		}
		userList.add(user);
	}

	public static User createInstance(RegistrationForm registrationForm,
			int userId) {
		User user = new User();

		user.setId(userId);
		user.setEmail(registrationForm.getEmail());
		user.setPassword(registrationForm.getPassword());
		user.setAge(registrationForm.getAge());
		user.setTipoUsuario(registrationForm.getTipoUsuario());
		user.setSignUpDate(DateUtils.getNow());

		return user;
	}

	public static boolean areCredentialsCorrect(String email, String pwd) {
		User user = new User();

		user.setEmail(email);
		user.setPassword(pwd);

		return getUserList().contains(user);
	}
	
	public static int getId(String email, String pwd) {
		User user = new User();

		user.setEmail(email);
		user.setPassword(pwd);

		return getUserList().indexOf(user);
	}

	public static User createInstance(RegistrationForm registrationForm) {
		return createInstance(registrationForm, registrationForm.getId());
	}

	public static void updateUser(User anUser) throws DBException {
		int i = getUserList().lastIndexOf(anUser);

		if (i != -1) {
			User user = getUserList().get(i);
			user.setEmail(anUser.getEmail());
			user.setPassword(anUser.getPassword());
			user.setAge(anUser.getAge());
			user.setTipoUsuario(anUser.getTipoUsuario());
		} else {
			throw new DBException(
					String.format(
							"updateUser. The user with email='%s' and password='%s' doesn't exit.",
							anUser.getEmail(), anUser.getPassword()));
		}

	}

	public static void testHibernate(SessionFactory sessionFactory) throws Exception {

		User user = new User();

		user.setId(-100);
		user.setEmail("user@test.com");
		user.setPassword("fakedPwd");
		user.setAge(40);
		user.setSignUpDate(new Date());

		Session session = null;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();

		} catch (HibernateException e) {
			logger.error("testHibernate no se pudo salvar el user de pruebas !!!", e);
			throw new Exception("testHibernate no se pudo salvar el user de pruebas !!!",e);
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

}
